<template>
  <div class="table-container">
    <div class="table-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>资讯公告</el-breadcrumb-item>
        <el-breadcrumb-item>公告列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table-search">
      卖场：
      <el-select
        v-model="marketId"
        clearable
        @change="selectMarket"
        filterable
        placeholder="请选择卖场"
      >
        <el-option
          v-for="item in marketList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-row style="margin-left:10px;">
        <el-input
          style="width: 200px;margin-right: 10px"
          placeholder="请输入公告信息查询"
          v-model="keyword"
          clearable
          @clear="queryList"
        >
        </el-input>
        <el-button type="primary" @click="queryList">搜索</el-button>
      </el-row>
    </div>
    <div class="table-content">
      <div class="table-bar">
        <el-button type="primary" @click="handleAdd">新增公告</el-button>
      </div>
      <el-table
        border
        empty-text="当前数据空空如也"
        ref="multipleTable"
        :data="notices"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="sort" label="排序" width="60"></el-table-column>
        <el-table-column prop="title" label="名称"></el-table-column>
        <el-table-column prop="is_pub" label="发布状态">
          <template slot-scope="scope">
            <p v-if="scope.row.is_pub == 1">已发布</p>
            <p v-else-if="scope.row.is_pub == 0">未发布</p>
          </template>
        </el-table-column>
        <el-table-column prop="ctime" label="创建日期">
        </el-table-column>
        <el-table-column prop="market_name" label="所属卖场">
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              style="margin-right: 5px"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @confirm="handleDelete(scope.index,scope.row)"
            >
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-nav">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        page: 1,
        pageSize: 10,
        total: 0,
        keyword: '',
        notices: [],
        multipleSelection: [],
        marketList:[], //卖场列表
        marketId:'', //已选卖场id
      }
    },
    mounted() {
      this.queryList();
      this.remoteMethod()
    },
    methods: {
      // 获取卖场列表
      remoteMethod() {
        this.$http.post("/api/root/Market/marketList").then((res) => {
          console.log(res, "卖场列表");
          if (res.code == 1) {
            this.marketList = res.data ? res.data : [];
          }
        });
      },
      // 选择卖场
      selectMarket() {
        this.page = 1
        this.pageSize = 10
        this.queryList();
      },

      handleAdd() {
        this.$router.push({name: 'noticeAdd'})
      },
      handleEdit(index, row) {
        const id = row.id;
        this.$router.push({name: 'noticeEdit', query: {id}});
      },
      handleDelete(index, row) {
        this.toDelete(row.id);
      },
      handleBatchDel() {
        if (this.multipleSelection.length < 1) {
          this.$message.error("请先勾选需要删除的公告！");
          return;
        }
        this.$confirm('确定要执行批量删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.toDelete(this.multipleSelection.join(","))
        }).catch(() => {
          console.log('放弃删除')
        });

      },
      selectedRow(selection, row) {
        this.multipleSelection = [];
        const _ths = this;
        selection.map(function (item) {
          _ths.multipleSelection.push(item.id);
        })
      },
      selectedAll(rows) {
        if (rows.length === 0) {
          this.multipleSelection = [];
        } else {
          const _ths = this;
          rows.map(function (item) {
            if (!_ths.multipleSelection.includes(item.id)) {
              _ths.multipleSelection.push(item.id);
            }
          })
        }
      },

      handleSizeChange(val) {
        this.pageSize = val;
        this.page = 1;
        this.queryList()
      },
      handleCurrentChange(val) {
        this.page = val;
        this.queryList();
      },
      queryList() {
        this.$http.get('/api/root/Notice/index', {
          page: this.page,
          pagesize: this.pageSize,
          keyword: this.keyword,
          market_id:this.marketId
        }).then(rest => {
          this.notices = rest.data.notices;
          this.total = rest.data.count;
        })
      },
      toDelete(id_lst) {
        this.$http.post('/api/root/Notice/del', {
          id: id_lst,
        }).then(rest => {
          if (rest.msg === 'ok') {
            this.queryList();
            this.multipleSelection = [];
            this.$message.success('删除成功');
          } else {
            this.$message.error("删除失败");
          }
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  @import 'index.less';
</style>
